<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accounts extends BIT_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function __construct()
	{
		parent::__construct();
		$this->header_views = array('admin/header');
		$this->footer_views = array('admin/footer');

		// Default message is empty
		$this->view_data['message'] = "";
		
		// Check if user is loggedin
		if(! $this->isloggedin())
		{
			redirect('admin/login');
		}
	}
	private function isloggedin(){return (isset($this->session->userdata) && $this->session->userdata('loggedin')===TRUE);}

	public function index()
	{
		$this->view_data['accounts'] = $this->db->get('accounts')->result();
		$this->v('admin/accounts');
	}

	public function remove($userid){

		$removed = $this->db->delete('accounts',array('id' => $userid));
		$this->index();
	}

	public function add(){
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$form_rules = array(
			array(
				'field' => 'email',
				'label' => 'Epost',
				'rules' => 'trim|required|valid_email'
			),
			array(
				'field' => 'pass1',
				'label' => 'Lösenord',
				'rules' => 'trim|required|matches[pass2]|min_length[5]'
			)
		);
		$this->form_validation->set_message('valid_email','Epostadressen är inte giltig');
		$this->form_validation->set_message('required','Du måste fylla i fältet');
		$this->form_validation->set_message('min_length','Ange minst 5 tecken');

		$this->form_validation->set_rules($form_rules);
		
		if($this->form_validation->run() == TRUE) {
			// Set message and new email
			$this->view_data['message'] = "Ändringar sparade";
			$data = array(
				'email'=>$this->input->post('email')
			);

			// Update password?
			if(strlen($this->input->post('pass1'))>0){
				$data['password'] = md5($this->input->post('pass1'));
			}
			$data = array('email' => $this->input->post('email'), "password" => md5($this->input->post('pass1')));
			$this->db->insert('accounts', $data);

			$this->index();
		}
		else{
			$this->v('admin/add_account');
		}
	}

	public function edit($userid){
		
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$form_rules = array(
			array(
				'field' => 'email',
				'label' => 'Epost',
				'rules' => 'trim|required|valid_email'
			),
			array(
				'field' => 'pass1',
				'label' => 'Lösenord',
				'rules' => 'matches[pass2]|min_length[5]'
			)
		);
		$this->form_validation->set_message('valid_email','Epostadressen är inte giltig');
		$this->form_validation->set_message('required','');
		$this->form_validation->set_message('valid_email','Epostadressen är inte giltig');

		$this->form_validation->set_rules($form_rules);
		
		if($this->form_validation->run() == TRUE) {
			// Set message and new email
			$this->view_data['message'] = "Ändringar sparade";
			$data = array(
				'email'=>$this->input->post('email')
			);

			// Update password?
			if(strlen($this->input->post('pass1'))>0){
				$data['password'] = md5($this->input->post('pass1'));
			}

			$this->db->where('id',$userid);
			$this->db->update('accounts', $data);
		}
		
		$users = $this->db->get_where('accounts', array('id' => $userid))->result();
		$this->view_data['user'] = $users[0];
		$this->v('admin/edit_account');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */